﻿using System;

namespace UniRx.Operators
{
	// Token: 0x020002E6 RID: 742
	internal class ScanObservable<TSource, TAccumulate> : OperatorObservableBase<TAccumulate>
	{
		// Token: 0x06000FC7 RID: 4039 RVA: 0x00048FD0 File Offset: 0x000473D0
		public ScanObservable(IObservable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator) : base(source.IsRequiredSubscribeOnCurrentThread<TSource>())
		{
			this.source = source;
			this.seed = seed;
			this.accumulator = accumulator;
		}

		// Token: 0x06000FC8 RID: 4040 RVA: 0x00048FF3 File Offset: 0x000473F3
		protected override IDisposable SubscribeCore(IObserver<TAccumulate> observer, IDisposable cancel)
		{
			return this.source.Subscribe(new ScanObservable<TSource, TAccumulate>.Scan(this, observer, cancel));
		}

		// Token: 0x04000932 RID: 2354
		private readonly IObservable<TSource> source;

		// Token: 0x04000933 RID: 2355
		private readonly TAccumulate seed;

		// Token: 0x04000934 RID: 2356
		private readonly Func<TAccumulate, TSource, TAccumulate> accumulator;

		// Token: 0x020002E7 RID: 743
		private class Scan : OperatorObserverBase<TSource, TAccumulate>
		{
			// Token: 0x06000FC9 RID: 4041 RVA: 0x00049008 File Offset: 0x00047408
			public Scan(ScanObservable<TSource, TAccumulate> parent, IObserver<TAccumulate> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
				this.isFirst = true;
			}

			// Token: 0x06000FCA RID: 4042 RVA: 0x00049020 File Offset: 0x00047420
			public override void OnNext(TSource value)
			{
				if (this.isFirst)
				{
					this.isFirst = false;
					this.accumulation = this.parent.seed;
				}
				try
				{
					this.accumulation = this.parent.accumulator(this.accumulation, value);
				}
				catch (Exception error)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
					return;
				}
				this.observer.OnNext(this.accumulation);
			}

			// Token: 0x06000FCB RID: 4043 RVA: 0x000490C4 File Offset: 0x000474C4
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000FCC RID: 4044 RVA: 0x000490FC File Offset: 0x000474FC
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x04000935 RID: 2357
			private readonly ScanObservable<TSource, TAccumulate> parent;

			// Token: 0x04000936 RID: 2358
			private TAccumulate accumulation;

			// Token: 0x04000937 RID: 2359
			private bool isFirst;
		}
	}
}
